//添加视频课程
exports.addOrganizationVideo = function(req,res) {
//平台通知列表
    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;
    var ov_id = 0;
    var ov_skin_id = bodyParams.ov_skin_id;    //皮肤id
    var ov_title_image = bodyParams.ov_title_image;    //封面图
    var ov_title_imgae_th = bodyParams.ov_title_imgae_th;    //封面缩略图
    var ov_title_image_text = bodyParams.ov_title_image_text;    //封面图上的文字
    var ov_title_image_text_style = bodyParams.ov_title_image_text_style;    //封面图上的文字样式
    var ov_title = bodyParams.ov_title;    //标题
    var ov_old_price = bodyParams.ov_old_price;    //原价
    var ov_now_price = bodyParams.ov_now_price;    //现价
    var ov_category_text = bodyParams.ov_category_text;    //类别名称
    var ov_category_id = bodyParams.ov_category_id;    //类别名称id
    var ov_course_style = bodyParams.ov_course_style;   //课程形式 0-试听 1-大师  2-常规'
    var ov_course_type = bodyParams.ov_course_type;    //课程类型   0 单次课  1 系列课程
//  var ov_tags = bodyParams.ov_tags;    //标签
    var ov_coupon_id = bodyParams.ov_coupon_id;    //代金券id
    var ov_coupon_type = bodyParams.ov_coupon_type;    //代金劵的类型 0-代金劵 1-约课劵 2-兑换卷
    var ov_video_address = bodyParams.ov_video_address;    //视频地址
    var ov_oid = bodyParams.ov_oid;    //所属机构id
    var ov_series_time = bodyParams.ov_series_time;    //如果是系列课程  这里要次数
    var ov_video_time = bodyParams.ov_video_time;    //视频时间长度
    var video_content = bodyParams.video_content;    //这里是内容拼接好的数组
    var ov_teacher_name = bodyParams.ov_teacher_name;    //这里是内容拼接好的数组

    //最终结果
    var resultData = {};

    //任务流创建
    async.waterfall([

        //获取视频的基本信息
        function(cb){
    	console.log('获取视频的基本信息')
    	console.log(CORE_APIConfig.coreAPI.addOrganizationVideo)
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.addOrganizationVideo,
                    method : 'POST',
                    data:{
                        "ov_skin_id":ov_skin_id,
                        "ov_title_image":ov_title_image,
                        "ov_title_imgae_th":ov_title_imgae_th,
                        "ov_title_image_text":ov_title_image_text,
                        "ov_title_image_text_style":ov_title_image_text_style,
                        "ov_title":ov_title,
                        "ov_old_price":ov_old_price,
                        "ov_now_price":ov_now_price,
                        "ov_category_text":ov_category_text,
                        "ov_category_id":ov_category_id,
                        "ov_course_style":ov_course_style,
                        "ov_course_type":ov_course_type,
                        "ov_coupon_id":ov_coupon_id,
                        "ov_coupon_type":ov_coupon_type,
                        "ov_video_address":ov_video_address,
                        "ov_oid":ov_oid,
                        "ov_series_time":ov_series_time,
//                      "ov_tags":ov_tags,
                        "ov_teacher_name":ov_teacher_name,
                        "ov_video_time":ov_video_time,
                    }
                },
                function (err, data) {

                    if (err) {
                    	console.log(err)
                    	console.log(data)
                        return cb(err, data);
                    }
                    ov_id = data
                    console.log("*****");
                    console.log(ov_id);
                    console.log("*****");
                    resultData.addData = data;
                    cb();
                }
            );
        },
        //判断是什么课程
        function(cb){
    	console.log('判断是什么课程')
            if(ov_course_type == 1){//这里是系列课程
                if(ov_series_time < 1){
                    RES.response(res,false,"请输入正确的集数");
                }
                //根据用户传的几 我先拼接一个长度为几的数组
                var tempArr = [];
                for(var i = 1; i<=ov_series_time ; i++){
                    var tempJson = {"ovs_order":i};
                    tempArr.push(tempJson) ;
                }
                //这里已经拿到了创建好的数组  循环添加

                var tasks = [];

                //循环节点列表
                tempArr.forEach(function (item, i) {


                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url    : CORE_APIConfig.coreAPI.addOrganizationVideoSeries,
                                method : 'POST',
                                data : {
                                    "ovs_ovid":ov_id,
                                    "ovs_title":"",
                                    "ovs_order":item.ovs_order,
                                }
                            },
                            function (err, data) {
                                //console.log("拼团");
                                if(err){
                                    return cb(err, data);
                                }
                                cb();
                            }
                        );

                    });

                });
                async.waterfall(tasks, cb);
            }else{
                cb();
            }
        },
        //上传课程内容
        function(cb){
    	console.log('上传课程内容')
            var tasks = [];

            //循环节点列表
            video_content.forEach(function (item, i) {
                //创建每一个插入任务
                tasks.push(function (cb) {

                    R.SEND_HTTP(
                        req,
                        {
                            url    : CORE_APIConfig.coreAPI.addOrganizationVideoContent,
                            method : 'POST',
                            data : {
                                "ovc_ovid":ov_id,
                                "ovc_type":item.type,
                                "ovc_order":item.order,
                                "ovc_content":item.content,
                            }
                        },
                        function (err, data) {
                            //console.log("拼团");
                            if(err){
                                return cb(err, data);
                            }
                            cb();
                        }
                    );

                });

            });
            async.waterfall(tasks, cb);
        }

    ],function(err, result){
    	
    	console.log('2222222222222222222222222222222')

        //返回最终拼接结果
        RES.response(res,true,resultData);

    });

}
